/**

 @Name：layuiAdmin 内容系统
 @Author：star1029
 @Site：http://www.layui.com/admin/
 @License：LPPL
    
 */


layui.define(['table', 'form'], function(exports){
  var $ = layui.$
  ,admin = layui.admin
  ,view = layui.view
  ,table = layui.table
  ,form = layui.form;

  //会员管理
  table.render({
    
    elem: '#LAY-app-content-list'
    ,url: '/member/findPage' //数据接口
    ,method:'post'
    ,contentType:'application/json'
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field: 'id', title: '序号', sort: true}
      ,{field: 'cardNo',minWidth: 110, title: '会员卡号'}
      ,{field: 'name', title: '姓名'}
      ,{field: 'sex', title: '性别'}
      ,{field: 'category', title: '身份'}
      ,{field: 'card',minWidth: 180, title: '身份证号'}
      ,{field: 'department', title: '系'}
      ,{field: 'profession', title: '专业'}
      ,{field: 'grade', title: '年级'}
      ,{field: 'phone',minWidth: 120, title: '电话'}
      ,{field: 'regdate', minWidth: 160,title: '入会时间'}
      ,{field: 'status', title: '状态'}
      ,{field: 'remark', title: '备注'}
     
      ,{title: '操作', minWidth: 150, align: 'center', fixed: 'right', toolbar: '#table-content-list'}
    ]]
    ,page: true
    ,limit: 10
    ,limits: [10, 15, 20, 25, 30]
    ,text: '对不起，加载出现异常！'
  });
  
  //监听工具条
  table.on('tool(LAY-app-content-list)', function(obj){
    var data = obj.data;
    var id = data.id;
    if(obj.event === 'del'){
      layer.confirm('确定删除此文章？', function(index){
        $.ajax({
          url: "/member/" + id,
          type: "delete",
          datatype: JSON,
          async: false,
          data: { data: id },
          success: function (res) {
            layer.msg("操作成功");
            admin.events.refresh();
          }

        });
        obj.del();
        layer.close(index);
      });
    } else if(obj.event === 'edit'){
      admin.popup({
        title: '编辑文章'
        ,area: ['550px', '550px']
        ,id: 'LAY-popup-content-edit'
        ,success: function(layero, index){
          view(this.id).render('app/content/listform', data).done(function(){
            form.render(null, 'layuiadmin-app-form-list');
             //回显数据
             form.val("layuiadmin-app-form-list", data);
            //监听提交
            form.on('submit(layuiadmin-app-form-submit)', function(data){
              var field = data.field; //获取提交的字段

              $.ajax({
                url:"/member/saveMember"
                ,type:'post'
                ,async:false
                ,data:JSON.stringify(field)
                ,contentType:'application/json'
                ,dateType:'json'
                ,success:function(data){
                  admin.events.refresh();
                }
              })
              //提交 Ajax 成功后，关闭当前弹层并重载表格
              //$.ajax({});
              layui.table.reload('LAY-app-content-list'); //重载表格
              layer.close(index); //执行关闭 
            });
          });
        }
      });
    }
  });

  //分类管理
  table.render({
    elem: '#LAY-app-content-tags'
    ,url: '/readroom/findAll' //模拟接口
    ,cols: [[
      {type: 'numbers', fixed: 'left'}
      ,{field: 'id', width: 100, title: 'ID'}
      ,{field: 'name', title: '资源名称'}
      ,{title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#layuiadmin-app-cont-tagsbar'}
    ]]
    ,text: '对不起，加载出现异常！'
  });
  
  //监听工具条
  table.on('tool(LAY-app-content-tags)', function(obj){
    var data = obj.data;
    var id = data.id;
    if(obj.event === 'del'){
      layer.confirm('确定删除此分类？', function(index){
        $.ajax({
          url: "/readroom/" + id,
          type: "delete",
          datatype: JSON,
          async: false,
          data: { data: id },
          success: function (res) {
            layer.msg("操作成功");
            admin.events.refresh();
          }

        });
        
        obj.del();
        layer.close(index);
      });
    } else if(obj.event === 'edit'){
      admin.popup({
        title: '编辑分类'
        ,area: ['450px', '200px']
        ,id: 'LAY-popup-content-tags'
        ,success: function(layero, index){
          view(this.id).render('app/content/tagsform', data).done(function(){
            form.render(null, 'layuiadmin-form-tags');
            //回显数据
            form.val("layuiadmin-app-cont-submit", data);
            //监听提交
            form.on('submit(layuiadmin-app-cont-submit)', function(data){
              var field = data.field; //获取提交的字段
              $.ajax({
                url:"/readroom/addOredit"
                ,type:'post'
                ,async:false
                ,data:JSON.stringify(field)
                ,contentType:'application/json'
                ,dateType:'json'
                ,success:function(data){
                  admin.events.refresh();
                }
              })
              //提交 Ajax 成功后，关闭当前弹层并重载表格
              //$.ajax({});
              layui.table.reload('LAY-app-content-tags'); //重载表格
              layer.close(index); //执行关闭 
            });
          });
        }
      });
    }
  });

  //日志管理
  table.render({
    elem: '#LAY-app-content-comm'
    ,url: '/consume/findAll' //模拟接口
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field: 'id', width: 100}
      ,{field: 'cardNo', title: '会员卡号'}
      ,{field: 'readRoomId', title: '资源编号'}
      ,{field: 'inTime', title: '进入时间'}
      ,{field: 'outTime', title: '退出时间'}
      ,{title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-content-com'}
    ]]
    ,page: true
    ,limit: 10
    ,limits: [10, 15, 20, 25, 30]
    ,text: '对不起，加载出现异常！'
  });
  
  //监听工具条
  table.on('tool(LAY-app-content-comm)', function(obj){
    var data = obj.data;
    if(obj.event === 'del'){
      layer.confirm('确定删除此条评论？', function(index){
        obj.del();
        layer.close(index);
      });
    } else if(obj.event === 'edit'){
      admin.popup({
        title: '编辑评论'
        ,area: ['450px', '300px']
        ,id: 'LAY-popup-content-comm'
        ,success: function(layero, index){
          view(this.id).render('app/content/contform', data).done(function(){
            form.render(null, 'layuiadmin-form-comment');
            
            //监听提交
            form.on('submit(layuiadmin-app-com-submit)', function(data){
              var field = data.field; //获取提交的字段

              //提交 Ajax 成功后，关闭当前弹层并重载表格
              //$.ajax({});
              layui.table.reload('LAY-app-content-comm'); //重载表格
              layer.close(index); //执行关闭 
            });
          });
        }
      });
    }
  });

  exports('contlist', {})
});